package com.business_clean.ui.activity;

import androidx.annotation.Nullable;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;

import com.blankj.utilcode.util.LogUtils;
import com.business_clean.R;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.uitl.GlideUtil;
import com.business_clean.databinding.ActivityPreviewPhotoBinding;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import me.hgj.mvvmhelper.base.BaseDbActivity;
import me.hgj.mvvmhelper.base.BaseViewModel;
import me.hgj.mvvmhelper.base.BaseVmActivity;

/**
 * 图片 - 视频 预览界面 目前只有视频
 */
public class PreviewPhotoActivity extends BaseActivity<BaseViewModel, ActivityPreviewPhotoBinding> {


    private String url;
    private String cover_url;//不一定用到，谁用谁去拿
    private boolean isVideo;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

        // 获取传递的参数
        url = getIntent().getStringExtra("url");
        cover_url = getIntent().getStringExtra("cover_url");
        isVideo = getIntent().getBooleanExtra("isVideo", false);
        LogUtils.e("预览的 url " + url);

        if (isVideo) {
            showVideo(url);
        } else {
            showImage(url);
        }
    }


    private void showImage(String imageUrl) {
        mDatabind.tabbar.setTitle("预览图片");
        mDatabind.imageView.setVisibility(View.VISIBLE);
        mDatabind.videoPlayer.setVisibility(View.GONE);
        GlideUtil.loadPic(this, imageUrl, mDatabind.imageView);
    }

    private void showVideo(String videoUrl) {
        mDatabind.tabbar.setTitle("预览视频");
        mDatabind.imageView.setVisibility(View.GONE);
        mDatabind.videoPlayer.setVisibility(View.VISIBLE);

        mDatabind.videoPlayer.setUp(url, true, "");

        //设置返回键
        mDatabind.videoPlayer.getBackButton().setVisibility(View.GONE);
        //全屏幕
        mDatabind.videoPlayer.getFullscreenButton().setVisibility(View.GONE);


        ///不需要屏幕旋转
        mDatabind.videoPlayer.setNeedOrientationUtils(false);
        mDatabind.videoPlayer.startPlayLogic();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDatabind.videoPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mDatabind.videoPlayer.onVideoResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }

}